<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vscode自定义代码生成器</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
      border-radius: 10px;
      margin: 1px;
    }

    body {
      background-color: #006eb4;
    }

    header {
      text-align: center;
    }

    .half {
      width: 50%;
      border: 4px solid #bfbfbf;
      height: 2000px;
      background-color: whitesmoke;
    }

    main {
      display: flex;
      margin: 10px;
    }

    .code {
      padding: 16px;
      background-color: beige;
      border: 6px solid #bfdbec;
      width: 100%;
      margin-top: 16px;
      height: 1000px;
      font-family: inherit;
      margin-top: 0px;
    }

    .code_input {
      width: 100%;
      padding: 16px;
      font-size: 18px;
    }

    .copy {
      background-color: #006eb4;
      padding: 16px;
      color: whitesmoke;
      cursor: pointer;
      width: 100%;
      font: 1em sans-serif;
      font-size: 16px;
    }
  </style>
</head>

<body>
  <header style="font-size: 24px;color: whitesmoke;">
    vscode自定义代码生成器
  </header>
  <main>
    <div class="half">
      <div>
        <div>
          <input id="description" class="code_input" type="text" placeholder="自定义代码块描述...">
          <input id="key" class="code_input" type="text" placeholder="关键字...">
        </div>
        <div></div>
      </div>
      <div>
        <textarea class="code_input" name="目标代码块s" id="code_input" cols="60" rows="80"
          placeholder="想要生成的代码块的样子..."></textarea>
      </div>
    </div>
    <div class="half">
      <div style="text-align: center;font-size: 42px;">
        vscode
      </div>
      <div>
        <button class="copy" onclick="copyUrl2()">复制代码提示块</button>
        <pre id="code" class="code"></pre>
      </div>
    </div>
  </main>
  <script>
    function copyUrl2() {
      var Url2 = document.getElementById("code").innerText;
      var oInput = document.createElement('input');
      oInput.value = Url2;
      document.body.appendChild(oInput);
      oInput.select(); // 选择对象
      document.execCommand("Copy"); // 执行浏览器复制命令
      oInput.className = 'oInput';
      oInput.style.display = 'none';
      alert('复制成功');
    }

    var obj = {
      test: "1234",
      key: "",
      code_input: "",
      description: ""
    };
    var newSnippet = ''
    function goFlash() {
      var snippet = document.getElementById("code_input").value;
      var separatedSnippet = snippet
        .replace(/\\/g, "\\\\")
        .replace(/"/g, '\\"')
        .split("\n");
      var separatedSnippetLength = separatedSnippet.length;

      // add double quotes around each line apart from the last one
      newSnippet = separatedSnippet.map((line, index) => {
        return index === separatedSnippetLength - 1 ? `"${line}"` : `"${line}",`;
      });

    }
    //主要使用到了get和set方法，最为关键
    Object.defineProperty(obj, "code", {
      get: function () {
      },
      set: function (val) {
        document.getElementById("code").innerText = val
      }
    });
    document.getElementById("description").addEventListener("keyup", function (event) {
      console.log('description: ', event.target.value);
      obj.description = event.target.value;
      goFlash()
      obj.code = `
    "${obj.description}": {
      "prefix": "${obj.key}",
      "body": [
          ${newSnippet.join('\n')}
      ],
      "description": "${obj.description}"
    }
  `;

    }
    )
    document.getElementById("key").addEventListener("keyup", function (event) {
      console.log('key: ', event.target.value);
      obj.key = event.target.value;
      goFlash()
      obj.code = `
    "${obj.description}": {
      "prefix": "${obj.key}",
      "body": [
          ${newSnippet.join('\n')}
      ],
      "description": "${obj.description}"
    }
  `;
    }
    )
    document.getElementById("code_input").addEventListener("keyup", function (event) {
      console.log('code_input: ', event.target.value);
      obj.code_input = event.target.value;
      goFlash()
      obj.code = `
    "${obj.description}": {
      "prefix": "${obj.key}",
      "body": [
          ${newSnippet.join('\n')}
      ],
      "description": "${obj.description}"
    }
  `;
    }
    )
  </script>
</body>

</html>